const isProduction = process.env.NODE_ENV === 'production' // 是否在正式环境
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const productionGzipExtensions = ['js', 'css']
const path = require('path')
function resolve (dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  publicPath: process.env.VUE_APP_PUBLIC_PATH,
  productionSourceMap: false,
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "~eve-ui/src/style/admin/base.scss";'
      }
    },
    extract: true,
    sourceMap: false
  },

  chainWebpack: (config) => {
    config.plugin('html').tap((args) => {
      args[0].title = '基础框架'
      return args
    })

    if (process.env.NODE_ENV === 'development') {
      config.plugin('hardSource').use(HardSourceWebpackPlugin)
      // 对下面配置的 module 不进行缓存
      config
        .plugin('hardSourceExcludeModule')
        .use(HardSourceWebpackPlugin.ExcludeModulePlugin, [
          [
            {
              test: /mini-css-extract-plugin[\\/]dist[\\/]loader/
            },
            {
              test: /eslint-loader/
            },
            {
              test: /.*\.DS_Store/
            }
          ]
        ])

      config.module
        .rule('fonts')
        .use('url-loader')
        .loader('url-loader')
        .options({})
        .end()
      config.module
        .rule('images')
        .use('url-loader')
        .loader('url-loader')
        .options({})
        .end()
    }
  },

  configureWebpack: (config) => {
    isProduction &&
      config.plugins.push(
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        })
      )
    config.resolve.alias['@asset'] = resolve('src/assets')
    config.resolve.alias['@'] = resolve('src')
    config.resolve.alias._c = resolve('src/components')
  },
  devServer: {
    open: false,
    disableHostCheck: true, // 防止 invalid host header
    compress: true,
    overlay: {
      // 出现编译器错误或警告时，在浏览器中显示全屏覆盖层
      warnings: false,
      errors: true
    },
    // historyApiFallback: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8080', // 后端接口地址
        changeOrigin: true, // 是否启动代理(是否跨域)
        secure: false, // 如果是https接口，需要配置这个参数
        ws: true, // 是否启用websockets;
        pathRewrite: {
          '^/api': ''
        }
      },
      // 静态资源代理
      '/static-resources': {
        target: 'http://192.168.180.106:47080' // --开发线
      }

      // '/upms-center': {
      //     target: 'http://192.168.180.106:47080/kong/kong-gateway/upms-center', // 后端接口地址
      //     changeOrigin: true, // 是否启动代理(是否跨域)
      //     secure: false, // 如果是https接口，需要配置这个参数
      //     ws: true, // 是否启用websockets;
      //     pathRewrite: {
      //         '^/upms-center': ''
      //     }
      // }
    }
  },
  transpileDependencies: ['eve-ui', 'element-ui']
}
